<template>
  <div class="com-container">
    <div class="title" :style="comStyle">
      <span>| {{ showTitle }}</span>
      <span
        :style="comStyle"
        class="iconfont title-icon"
        @click="showChoice = !showChoice"
      >
        {{ showChoice ? "&#xe6ee;" : "&#xe6eb;" }}
      </span>
      <div class="select-con" v-show="showChoice" :style="marginStyle">
        <div
          class="select-item"
          v-for="item in selectTypes"
          :key="item.key"
          @click="handleSelect(item.key)"
        >
          {{ item.text }}
        </div>
      </div>
    </div>

    <div class="com-chart" ref="trend_ref"></div>
  </div>
</template>

<script>
import { mapState } from "vuex";

export default {
  data() {
    return {
      chartInstance: null,
      allData: null, // 从服务器中获取到的所有数据
      showChoice: false, // 是否显示下拉列表
      choiceType: "map", // 显示的数据类型
      titleFontSize: 0, // 指明标题的字体大小
    };
  },
  watch: {
    theme() {
      console.log("主题切换了");
      this.chartInstance.dispose(); // 销毁当前的图表
      this.initChart(); // 以新主题切换图表对象
      this.screenAdapter(); // 完成屏幕适配
      this.updateChart(); // 更新图表展示
    },
  },
  computed: {
    ...mapState(["theme"]),
    // 下拉菜单数据列表
    selectTypes() {
      if (!this.allData) {
        return [];
      } else {
        // 当前类型不应该显示在列表中
        return this.allData.type.filter((item) => {
          return item.key != this.choiceType;
        });
      }
    },
    // 当前显示的下拉菜单item标题
    showTitle() {
      if (!this.allData) {
        return "";
      } else {
        return this.allData[this.choiceType].title;
      }
    },
    // 设置给标题的样式
    comStyle() {
      return {
        fontSize: this.titleFontSize + "px",
      };
    },
    // 设置给下拉列表其他item的margin
    marginStyle() {
      return {
        marginLeft: this.titleFontSize - 25 + "px",
      };
    },
  },
  mounted() {
    this.initChart();
    // this.getData();

    // 发送数据给服务器，告诉服务器,我现在需要数据
    this.$socket.send({
      action: "getData",
      socketType: "trendData",
      chartName: "trend",
      value: "",
    });

    window.addEventListener("resize", this.screenAdapter);
    this.screenAdapter();
  },
  created() {
    // 在组件创建完成之后,进行回调函数的注册
    this.$socket.registerCallback("trendData", this.getData);
  },
  destroyed() {
    window.removeEventListener("resize", this.screenAdapter);
    // 在组件销毁之后进行回调函数的取消
    this.$socket.unRegisterCallback("trendData");
  },
  methods: {
    initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.trend_ref, this.theme);
      const initOption = {
        grid: {
          left: "3%",
          top: "35%",
          right: "4%",
          bottom: "1%",
          containLabel: true, // 显示坐标轴文字(主题默认隐藏)
        },
        legend: {
          left: 20,
          top: "15%",
          icon: "circle",
        },
        tooltip: {
          trigger: "axis",
        },
        xAxis: {
          type: "category",
          boundaryGap: false, // 折线起点 和 y轴不需要间隔,直接连接
        },
        yAxis: {
          type: "value",
        },
      };
      this.chartInstance.setOption(initOption);
    },
    getData(ret) {
      this.allData = ret;

      this.updateChart();
    },
    updateChart() {
      // 半透明的颜色值
      const colorArr1 = [
        "rgba(11,168,44,0.5)",
        "rgba(44,110,255,0.5)",
        "rgba(22,242,217,0.5)",
        "rgba(254,33,30,0.5)",
        "rgba(250,105,0,0.5)",
      ];
      // 全透明的颜色值
      const colorArr2 = [
        "rgba(11,168,44,0)",
        "rgba(44,110,255,0)",
        "rgba(22,242,217,0)",
        "rgba(254,33,30,0)",
        "rgba(250,105,0,0)",
      ];

      // 处理数据
      // 类目轴的数据
      const timeArr = this.allData.common.month;
      // y轴的数据 series下的数据
      const valueArr = this.allData[this.choiceType].data;
      const seriesArr = valueArr.map((item, index) => {
        return {
          name: item.name, // 对应着图例的legend 上面的分类图标
          type: "line",
          data: item.data,
          stack: this.choiceType,
          areaStyle: {
            // 折线图面积填充
            // 渐变颜色
            color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
              {
                offset: 0,
                color: colorArr1[index],
              }, // 0%的颜色值
              {
                offset: 1,
                color: colorArr2[index],
              }, // 100%下的颜色值
            ]),
          },
        };
      });

      // 图例的数据
      const legendArr = valueArr.map((item) => {
        return item.name;
      });

      const dataOption = {
        xAxis: {
          data: timeArr,
        },
        legend: {
          // 图标分类bar
          data: legendArr,
        },
        series: seriesArr,
      };
      this.chartInstance.setOption(dataOption);
    },
    // 分辨率适配
    screenAdapter() {
      this.titleFontSize = (this.$refs.trend_ref.offsetWidth / 100) * 3.6; // 标题字体大小
      const adapterOption = {
        legend: {
          // 图例样式设置
          itemWidth: this.titleFontSize,
          itemHeight: this.titleFontSize,
          itemGap: this.titleFontSize, // 每个图例的间距
          textStyle: {
            fontSize: this.titleFontSize / 2, //图例文字
          },
        },
      };
      this.chartInstance.setOption(adapterOption);
      this.chartInstance.resize();
    },
    handleSelect(currentType) {
      this.choiceType = currentType;
      this.updateChart();
      this.showChoice = false;
    },
  },
};
</script>

<style lang="less" scoped>
.title {
  position: absolute;
  left: 20px;
  top: 20px;
  z-index: 10;
  color: white;

  .title-icon {
    margin-left: 10px;
    cursor: pointer;
  }

  .select-con {
    .select-item {
      cursor: pointer;
    }
  }
}
</style>